package com.ibm.cps.newmessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.esotericsoftware.kryo.KryoSerializable;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ibm.cps.message.MessageFields;
import com.ibm.cps.message.validation.ValidatorUtil;
import com.ibm.util.JSONUtility;
import com.ibm.util.exception.CPSException;

public abstract class AbstractMetadata implements KryoSerializable{

	protected String tenantid;
	protected String processorid;
	protected String processorType;
	protected ArrayList<String> parentProcessors;
	protected String input;
	protected HashMap<String, String> key2value;
	protected String groupid;
	protected String source;

	public AbstractMetadata() {

	}

	public AbstractMetadata(String input) throws CPSException {
		this(JSONUtility.fromObjectString(input));
	}

	public AbstractMetadata(ObjectNode object) throws CPSException {
		this.input = object.toString();
		key2value = JSONUtility.getKeyValue(input);
		this.processorType = key2value.get(MessageFields.TYPE);
		this.processorid = key2value.get(MessageFields.PROCESSORID);
		this.tenantid = key2value.get(MessageFields.TENANTID);
		this.groupid = key2value.get(MessageFields.GROUPID);
		this.source = key2value.get(MessageFields.SOURCE);

		String[] commonFields = { MessageFields.SOURCE, MessageFields.GROUPID,
				MessageFields.TYPE, MessageFields.TENANTID,
				MessageFields.PROCESSORID };
		ValidatorUtil.validateStringFields(object, commonFields);
	}

	public List<String> getParentProcessors() {
		return parentProcessors;
	}

	public HashMap<String, String> getKey2value() {
		return key2value;
	}

	public String getTenantid() {
		return tenantid;
	}

	public String getInput() {
		return input;
	}

	public String getProcessorid() {
		return processorid;
	}

	public String getInputFieldType() {
		return processorType;
	}

	public String getGroupid() {
		return groupid;
	}

	public String getSource() {
		return source;
	}

}
